package org.inspiration.dudu.web.service.user.impl;

import lombok.RequiredArgsConstructor;
import org.inspiration.dudu.common.constant.Constant;
import org.inspiration.dudu.common.constant.RedisConstant;
import org.inspiration.dudu.common.exception.ServiceException;
import org.inspiration.dudu.common.util.RedisUtil;
import org.inspiration.dudu.content.model.vo.AnswerVO;
import org.inspiration.dudu.content.model.vo.ArticleVO;
import org.inspiration.dudu.content.model.vo.CommentVO;
import org.inspiration.dudu.content.model.vo.QuestionVO;
import org.inspiration.dudu.content.service.AnswerService;
import org.inspiration.dudu.content.service.ArticleService;
import org.inspiration.dudu.content.service.CommentService;
import org.inspiration.dudu.content.service.QuestionService;
import org.inspiration.dudu.user.mapper.UserMapper;
import org.inspiration.dudu.user.model.dto.UserLikeDTO;
import org.inspiration.dudu.user.model.entity.UserLikeEntity;
import org.inspiration.dudu.user.service.UserLikeService;
import org.inspiration.dudu.user.service.UserService;
import org.inspiration.dudu.web.service.user.UserLikeFacadeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class UserLikeFacadeServiceImpl implements UserLikeFacadeService {

    private final UserLikeService userLikeService;
    private final ArticleService articleService;
    private final QuestionService questionService;
    private final AnswerService answerService;
    private final CommentService commentService;
    private final UserService userService;


    /**
     * 创建用户点赞
     * 
     * @param userLikeDTO 用户点赞数据传输对象
     */
    @Override
    @Transactional
    public void createUserLike(UserLikeDTO userLikeDTO) {

        // 创建点赞关系
        UserLikeEntity userLikeEntity = userLikeService.transToEntity(userLikeDTO);
        userLikeService.createUserLike(userLikeDTO);
        // 增加contentId的点赞数量
        String contentId = userLikeDTO.getContentId();
        switch (userLikeDTO.getContentType()) {
            case "article" -> {
                articleService.increaseLikeCount(contentId);
            }
            case "question" -> {
                questionService.increaseLikeCount(contentId);
            }
            case "answer" -> {
                answerService.increaseLikeCount(contentId);
            }
            case "comment" -> {
                commentService.increaseLikeCount(contentId);
            }
            default -> throw new ServiceException(Constant.CONTENT_TYPE_ERROR);
        };
        // 同时增加该内容发布者的获得赞的数量
        userService.increaseLikeCount(userLikeEntity);
    }

    /**
     * 删除用户点赞
     * 
     * @param userLikeDTO 用户点赞数据传输对象
     */
    @Override
    @Transactional
    public void deleteUserLike(UserLikeDTO userLikeDTO) {
        // 删除点赞关系
        UserLikeEntity userLikeEntity = userLikeService.getUserLike(userLikeDTO);
        userLikeService.deleteUserLike(userLikeDTO);
        // 减少contentId的点赞数量
        String contentId = userLikeDTO.getContentId();

        switch (userLikeDTO.getContentType()) {
            case "article" -> {
                articleService.decreaseLikeCount(contentId);
            }
            case "question" -> {
                questionService.decreaseLikeCount(contentId);
            }
            case "answer" -> {
                answerService.decreaseLikeCount(contentId);
            }
            case "comment" -> {
                commentService.decreaseLikeCount(contentId);
            }
            default -> throw new ServiceException(Constant.CONTENT_TYPE_ERROR);
        };
        // 减少对应发布者的赞的总数
        userService.decreaseLikeCount(userLikeEntity);
    }

    /**
     * 判断用户是否已点赞
     * 
     * @param userId 用户ID
     * @param contentId 内容ID
     * @return 是否已点赞
     */
    @Override
    public Boolean isLiked(Long userId, String contentId) {
        return userLikeService.isLiked(userId, contentId);
    }



    /**
     * 将点赞标记为已读
     */
    @Override
    public void markAsRead(UserLikeDTO userLikeDTO) {
        // 将点赞关系标记为已读
        userLikeService.markAsRead(userLikeDTO);
        // 减少用户未读量
        userService.decreaseUnreadLikeCount(userLikeDTO.getTargetUserId());

    }

    /**
     * 获取用户最近的点赞记录
     * 
     * @param userId 用户ID
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @return 用户最近的点赞记录
     */
    @Override
    public List<Map<String, Object>> getLikeContentsByUserIdOrderByDESC(Long userId, Integer pageNum, Integer pageSize) {
        List<UserLikeEntity> list = userLikeService.getLikesByUserIdOrderByDESC(userId, pageNum, pageSize);
        List<Map<String, Object>> res = new ArrayList<>();
        for(UserLikeEntity userLikeEntity : list) {
            String contentType = userLikeEntity.getContentType();
            String contentId = userLikeEntity.getContentId();
            LocalDateTime likedAt = userLikeEntity.getLikedAt();
            Map<String, Object> map = new HashMap<>();
            map.put("contentType", contentType);
            map.put("likedAt", likedAt);
            switch (contentType) {
                case Constant.ARTICLE_TYPE -> {
                    ArticleVO articleVO = articleService.getArticleById(contentId);
                    map.put("content", articleService.transToSimpleVO(articleVO));
                }
                case Constant.QUESTION_TYPE -> {
                    QuestionVO questionVO = questionService.getQuestionById(contentId);
                    map.put("content", questionVO);
                }
                case Constant.ANSWER_TYPE -> {
                    AnswerVO answerVO = answerService.getAnswerByContentId(contentId);
                    QuestionVO questionVO = questionService.getQuestionById(answerVO.getParentId());
                    map.put("content", answerVO);
                    map.put("question", questionVO);
                }
                default -> {
                    throw new ServiceException(Constant.CONTENT_TYPE_ERROR);
                }
            }
            res.add(map);
        }
        return res;
    }

    @Override
    public List<Map<String, Object>> getReceiveLikeContentsByUserIdOrderByDESC(Long userId, Integer pageNum, Integer pageSize) {
        List<Map<String, Object>> res = new ArrayList<>();
        List<UserLikeEntity> userLikeEntityList = userLikeService.getUserLikesByTargetUserIdOrderByDESC(userId, pageNum, pageSize);
        for(UserLikeEntity userLikeEntity : userLikeEntityList) {
            String contentType = userLikeEntity.getContentType();
            String contentId = userLikeEntity.getContentId();
            Map<String, Object> map = new HashMap<>();
            map.put("userLike", userLikeService.transToVO(userLikeEntity));
            map.put("contentType", contentType);
            switch (contentType) {
                case Constant.ARTICLE_TYPE -> {
                    ArticleVO articleVO = articleService.getArticleById(contentId);
                    map.put("content", articleService.transToSimpleVO(articleVO));

                }
                case Constant.QUESTION_TYPE -> {
                    QuestionVO questionVO = questionService.getQuestionById(contentId);
                    map.put("content", questionVO);
                }
                case Constant.ANSWER_TYPE -> {
                    AnswerVO answerVO = answerService.getAnswerByContentId(contentId);
                    QuestionVO questionVO = questionService.getQuestionById(answerVO.getParentId());
                    map.put("content", answerVO);
                    map.put("question", questionVO);
                }
                case Constant.COMMENT_TYPE -> {
                    CommentVO commentVO = commentService.transToVO(commentService.getCommentByContentId(contentId));
                    map.put("content", commentVO);
                }
            }
            res.add(map);
        }
        return res;
    }


}
